﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockFinder.UI.Views;
using StockFinder.UI.Controllers;

namespace StockFinder.Forms
{
    public partial class DownloadAllHistoricPricesForm : Form, IDownloadAllHistoricPricesView
    {
        private readonly DownloadAllHistoricPricesViewController _DownloadAllHistoricPricesViewController;

        public DownloadAllHistoricPricesForm()
        {
            _DownloadAllHistoricPricesViewController = new DownloadAllHistoricPricesViewController(this);

            InitializeComponent();
        }

        private void _StartButton_Click(object sender, EventArgs e)
        {
            OnStart();
        }

        protected virtual void OnStart()
        {
            if (Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }

        protected virtual void OnStop()
        {
            if (Stop != null)
            {
                Stop(this, EventArgs.Empty);
            }
        }

        #region IDownloadAllHistoricPricesView Members

        public void AppendLog(string logMessage)
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    if (_LogTextBox.TextLength > 1000)
                    {
                        //clear the textbox
                        _LogTextBox.Clear();
                        _LogTextBox.AppendText("Clearing log");
                        _LogTextBox.AppendText(Environment.NewLine);
                        _LogTextBox.SelectionStart = _LogTextBox.Text.Length;
                        _LogTextBox.ScrollToCaret();
                    }

                    //append to text box
                    _LogTextBox.AppendText(logMessage);
                    _LogTextBox.AppendText(Environment.NewLine);
                    _LogTextBox.SelectionStart = _LogTextBox.Text.Length;
                    _LogTextBox.ScrollToCaret();
                }));
            }
        }

        public void UpdateProgress(int minimum, int maximum, int value)
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    //update progress bar
                    _ProgressBar.Minimum = minimum;
                    _ProgressBar.Maximum = maximum;
                    _ProgressBar.Value = value;

                    //update items label
                    _ItemsCounterLabel.Text = string.Format("{0}/{1}", value, maximum);

                    //update % progress label
                    _PercentageProgressLabel.Text = string.Format("{0}%", (((decimal)value / (decimal)maximum) * 100).ToString("N2"));
                }));
            }
        }

        public void ResetProgress()
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    _ProgressBar.Value = 0;
                }));
            }
        }

        public void UpdateEstimatedTimeRemaining(TimeSpan timeRemaining)
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    _TimeRemainingLabel.Text = string.Format("ETA: {0:00}:{1:00}:{2:00}", timeRemaining.Hours, timeRemaining.Minutes, timeRemaining.Seconds);
                }));
            }
        }

        public void SetTimeTaken(double secondsTaken)
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    _TimeTakenLabel.Text = string.Format("{0:00}s", secondsTaken.ToString());
                }));
            }
        }

        public void SetAverageSecondsTaken(decimal averageSecondsTaken)
        {
            if (this.IsHandleCreated)
            {
                //make sure on main thread
                Invoke(new MethodInvoker(delegate
                {
                    _AverageSecondsLabel.Text = string.Format("Average: {0:00}s", averageSecondsTaken.ToString());
                }));
            }
        }

        public event EventHandler Start;
        public event EventHandler Stop;

        public DateTime GetDateFrom()
        {
            return _FromDatePicker.Value.Date;
        }

        public DateTime GetToDate()
        {
            return _ToDatePicker.Value.Date;
        }

        #endregion

        private void DownloadAllHistoricPricesForm_Load(object sender, EventArgs e)
        {
            _FromDatePicker.Value = DateTime.Now.AddYears(-1);
        }

        private void _StopButton_Click(object sender, EventArgs e)
        {
            OnStop();
        }
    }
}
